﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class EditHistoricalBalances : Form
    {
        Balance CurrentBalance = null;

        public EditHistoricalBalances()
        {
            InitializeComponent();
        }

        private void EditHistoricalBalances_Load(object sender, EventArgs e)
        {
            ddlFunds.DataSource = MainForm.projector.Funds;
        }

        public void SetFund(Fund fund)
        {
            ddlFunds.SelectedItem = fund;
        }

        public void ClearForm()
        {
            CurrentBalance = null;
            dtpBalanceDate.Value = DateTime.Today;
            txtBalanceAmount.Text = "";
            txtDescription.Text = "";
        }

        #region list/dropdown events

        private void lstBalances_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstBalances.SelectedItem == null) return;

            Balance bal = (Balance) lstBalances.SelectedItem;
            dtpBalanceDate.Value = bal.BalanceDate;
            txtBalanceAmount.Text = bal.Amount.ToString();
            txtDescription.Text = bal.Description;

            CurrentBalance = bal;
        }

        private void ddlFunds_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearForm();

            Fund fund = (Fund) ddlFunds.SelectedItem;

            if (fund == null) return;

            lstBalances.DataSource = fund.HistoricalBalances;
        }

        #endregion

        #region Button Handlers

        private void btnNew_Click(object sender, EventArgs e)
        {
            ClearForm();
        }

        private void btnUpdateBalance_Click(object sender, EventArgs e)
        {
            Fund fund = (Fund)ddlFunds.SelectedItem;
            if (fund == null)
            {
                MessageBox.Show("There is no fund selected to save balance to", "Unable to save Historical Balance", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (lstBalances.SelectedItem == null && CurrentBalance != null) return;

            Decimal Amount = 0.0m;

            try { Amount = Decimal.Parse(txtBalanceAmount.Text); }
            catch
            {
                MessageBox.Show("You need to enter a correct Balance Amount", "Unable to save Balance", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Balance bal;

            // If new, instantiate balance, else get reference
            if (CurrentBalance == null) bal = new Balance();
            else bal = (Balance)lstBalances.SelectedItem;

            bal.BalanceDate = dtpBalanceDate.Value;
            bal.Amount = Amount;
            bal.Description = txtDescription.Text;

            // If this is new balance, then add to fund's historic balance collection
            if (CurrentBalance == null) fund.HistoricalBalances.Add(bal);

            lstBalances.DataSource = null;
            lstBalances.DataSource = fund.HistoricalBalances;

            // Flag Universe as Changed 
            MainForm.InstanceRef.HasChanged = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstBalances.SelectedItem == null) return;

            Fund fund = (Fund)ddlFunds.SelectedItem;
            if (fund == null)
            {
                MessageBox.Show("There is no fund selected to remove balance from", "Unable to remove historical balance", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (MessageBox.Show("Are you sure you want to delete the Balance ?", "Confirm balance deletion", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) return;

            Balance bal = (Balance)lstBalances.SelectedItem;

            fund.HistoricalBalances.Remove(bal);

            // force list refresh
            lstBalances.DataSource = null;
            lstBalances.DataSource = fund.HistoricalBalances;
        }

        private void btnDone_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/AdvancedHistoricalBalances.aspx";
            proc.Start();
        }

    }
}
